﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Xml.Linq;
using EquipSecs300;
using SecsEquip;
using SecsEquip300;


namespace EquipSecs300
{
    public class GEM_Even : SecsEquipEx
    {
        public string m_pViewName;

        private GEM_Variables Variables { get; set; }
        private List<EventInfo> VIDlistInfo;
        public GEM_Even(CSecsEquip300 m_pSecs, SecsEquipHelper secsEquipHelper)
        {
            this.m_pSecs = m_pSecs;
            this.SecsEquipHelper = secsEquipHelper;
        }
        public override void UpdateData()
        {
            Variables = SecsEquipHelper.Variables;

            RefreshEventInfo(ref VIDlistInfo);
        }

        // 发送事件
        public void PosEvent(int nEvent)
        {
            COMM_STATE cOMM_STATE;
            m_pSecs.GetCommState(out cOMM_STATE);
            if (cOMM_STATE == COMM_STATE.eCOMM_DISABLED)
            {
                return;
            }
            Variables.UpdataEvenTime();
            if (nEvent != 0)
            {
                m_pSecs.EventPos(nEvent);
                LogEvenMsg(nEvent);
            }
        }

        // 获取事件列表
        void RefreshEventInfo(ref List<EventInfo> listInfo)
        {
            m_pSecs.EventGetList(ref listInfo);
        }


        private string GetEventName(int intVID)
        {
            foreach (EventInfo e in VIDlistInfo)
            {
                if (e.CEID == intVID.ToString())
                {
                    return e.Name;
                }
            }
            return "";
        }

        #region 

        private bool PMStart_B;
        public void Ev_PMStart_R()
        {
            PMStart_B = false;
        }
        /// <summary>
        /// PM_n 开始Start，上报PMStart事件
        /// </summary>
        public void Ev_PMStart()
        {
            if (PMStart_B) { return; }
            PosEvent(30003);
            PMStart_B = true;
        }
        private bool PMEnd_B;
        public void Ev_PMEnd_R()
        {
            PMEnd_B = false;
        }
        /// <summary>
        /// PM_n 开始End，上报PMEnd事件
        /// </summary>
        public void Ev_PMEnd()
        {
            if (PMEnd_B) { return; }
            PosEvent(30004);
            PMEnd_B = true;
        }

        #endregion

        #region 
        bool Ev_EQStatesChange_B;
        public void Ev_EQStatesChange_R()
        {
            Ev_EQStatesChange_B = false;
        }
        /// <summary>
        /// 设备状态改变
        /// </summary>
        /// <param name="deviceMode_KE"></param>
        /// <param name="deviceStatus_KE"></param>
        public void Ev_EQStatesChange(DeviceMode_KE deviceMode_KE, DeviceStatus_KE deviceStatus_KE)
        {
            //if (Ev_EQStatesChange_B) { return;}
            Variables.SetValue(10010, ((int)deviceMode_KE).ToString());
            Variables.SetValue(10011, ((int)deviceStatus_KE).ToString());
            PosEvent(30001);
            Ev_EQStatesChange_B = true;
        }


        bool Ev_EQDoorStatesB;
        public void Ev_EQDoorStates_R()
        {
            Ev_EQDoorStatesB = false;
        }
        /// <summary>
        /// 设备安全门状态
        /// </summary>
        /// <param name="efemEqSafetyDoorStatus_KE"></param>
        /// <param name="inspEqSafetyDoorStatus_KE"></param>
        public void Ev_EQDoorStates(EfemEqSafetyDoorStatus_KE efemEqSafetyDoorStatus_KE, InspEqSafetyDoorStatus_KE inspEqSafetyDoorStatus_KE)
        {
            //if (Ev_EQ_Door_States_B) { return; }
            Variables.SetValue(10110, ((int)efemEqSafetyDoorStatus_KE).ToString());
            Variables.SetValue(10111, ((int)inspEqSafetyDoorStatus_KE).ToString());
            PosEvent(30002);
            Ev_EQDoorStatesB = true;
        }

        bool Ev_EapState_B;
        public void Ev_EapState_R()
        {
            Ev_EapState_B = false;
        }
        /// <summary>
        /// EAP连线状态
        /// </summary>
        /// <param name="eAPConnectionStatus_KE"></param>
        public void Ev_EapState(EAPConnectionStatus_KE eAPConnectionStatus_KE)
        {
            //if (Ev_EAP_STATE_B) { return; }
            Variables.SetValue(10050, ((int)eAPConnectionStatus_KE).ToString());
            PosEvent(30005);
            Ev_EapState_B = true;
        }

        bool Ev_PortFoupStates_B;
        public void Ev_PortFoupStates_R()
        {
            Ev_PortFoupStates_B = false;
        }
        /// <summary>
        /// PORT FOUP状态PORT FOUP STATES
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        public void Ev_PortFoupStates(PortNO_KE portNO_KE, PortFoupStatus_KE loadPortFoupStatus_KE)
        {
            //if (Ev_PORT_FOUP_STATES_B) { return; }
            Variables.SetValue(10199, ((int)portNO_KE).ToString());
            Variables.SetValue(10200, ((int)loadPortFoupStatus_KE).ToString());
            PosEvent(30010);
            Ev_PortFoupStates_B = true;
        }

        bool Ev_PortFoupArrived_B;
        public void Ev_PortFoupArrived_R()
        {
            Ev_PortFoupArrived_B = false;
        }
        /// <summary>
        /// PORT FOUP状态PORT FOUP ARRIVED
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        public void Ev_PortFoupArrived(PortNO_KE portNO_KE, PortWaferStatus_KE portWaferStatus_KE)
        {
            if (Ev_PortFoupArrived_B) { return; }
            Variables.SetValue(10199, ((int)portNO_KE).ToString());
            Variables.SetValue(10200, ((int)portWaferStatus_KE).ToString());
            PosEvent(30060);
            Ev_PortFoupArrived_B = true;
        }

        bool Ev_PortFoupUnloadButLockd_B;
        public void Ev_PortFoupUnloadButLockd_R()
        {
            Ev_PortFoupUnloadButLockd_B = false;
        }
        /// <summary>
        /// PORT FOUP状态PORT FOUP UNLOAD BUT LOCK
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        public void Ev_PortFoupUnloadButLockd(PortNO_KE portNO_KE, PortWaferStatus_KE portWaferStatus_KE)
        {
            if (Ev_PortFoupUnloadButLockd_B) { return; }
            Variables.SetValue(10199, ((int)portNO_KE).ToString());
            Variables.SetValue(10200, ((int)portWaferStatus_KE).ToString());
            PosEvent(30061);
            Ev_PortFoupUnloadButLockd_B = true;
        }

        bool Ev_PortSlopMap_B;
        public void Ev_PortSlopMap_R()
        {
            Ev_PortSlopMap_B = false;

        }
        /// <summary>
        /// PORT FOUP 状态&ID&Mapping结果
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        /// <param name="mapRet"></param>
        public void Ev_PortSlopMap(PortNO_KE portNO_KE, PortFoupStatus_KE portWaferStatus_KE, string portFoupID, List<ushort> mapRet)
        {
            if (Ev_PortSlopMap_B) { return; }
            Variables.SetValue(10199, ((int)portNO_KE).ToString());
            Variables.SetValue(10200, ((int)portWaferStatus_KE).ToString());
            Variables.SetValue(10201, portFoupID);

            List<int> nVIDList = Variables.GetVIDList(10202, 10226);
            for (int i = 0; i < 25; i++)
            {
                if (i < mapRet.Count)
                {
                    Variables.SetValue(nVIDList[i], mapRet[i].ToString());
                }
                else
                {
                    Variables.SetValue(nVIDList[i], "0");
                }
            }
            PosEvent(30020);
            Ev_PortSlopMap_B = true;
        }

        bool Ev_RecipeChangeSucc_B;
        public void Ev_RecipeChangeSucc_R()
        {
            Ev_RecipeChangeSucc_B = false;

        }
        /// <summary>
        /// Recipe切换成功
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        /// <param name="mapRet"></param>
        public void Ev_RecipeChangeSucc(string recipeName, string lotNO, List<int> sampleList = null)
        {
            if (Ev_RecipeChangeSucc_B) { return; }
            Variables.SetValue(10350, recipeName);
            Variables.SetValue(10351, lotNO);

            if (sampleList != null && sampleList.Count > 0)
            {
                for (int i = 0; i < 13; i++)
                {
                    Variables.SetValue(10227 + i, sampleList[i].ToString());
                }
            }

            PosEvent(30025);
            Ev_RecipeChangeSucc_B = true;
        }


        bool Ev_FoupWaferFetchOut_B;
        public void Ev_FoupWaferFetchOut_R()
        {
            Ev_FoupWaferFetchOut_B = false;
        }
        /// <summary>
        /// ARM1从Foup取Wafer  ROBOT ARM1 WAFER 资料
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_FoupWaferFetchOut(SePresentData sePresentData)
        {
            if (Ev_FoupWaferFetchOut_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.Arm1, sePresentData);
            PosEvent(30031);
            Ev_FoupWaferFetchOut_B = true;
        }

        bool Ev_WaferBackFoup_B;
        public void Ev_WaferBackFoup_R()
        {
            Ev_WaferBackFoup_B = false;
        }
        /// <summary>
        /// ARM1 Wafer放回 Foup报告 ROBOT ARM1 WAFER 资料
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_WaferBackFoup(SePresentData sePresentData)
        {
            if (Ev_WaferBackFoup_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.Arm1, sePresentData);
            PosEvent(30032);
            Ev_WaferBackFoup_B = true;
        }

        //bool Ev_ARM2_Take_FOUP_Wafer_B;
        //public void Ev_ARM2_Take_FOUP_Wafer_R( )
        //{
        //    Ev_ARM2_Take_FOUP_Wafer_B = false;
        //}
        ///// <summary>
        ///// ARM2从Foup取Wafer
        ///// </summary>
        ///// <param name="sePresentData"></param>
        //public void Ev_ARM2_Take_FOUP_Wafer(SePresentData sePresentData)
        //{
        //    if (Ev_ARM2_Take_FOUP_Wafer_B) { return; }
        //    SetPresentValue(WorkStation_KE.Arm2, sePresentData);
        //    PosEvent(30033);
        //    Ev_ARM2_Take_FOUP_Wafer_B = true;
        //}

        //bool Ev_ARM2_Wafer_Back_FOUP_B;
        //public void Ev_ARM2_Wafer_Back_FOUP_R( )
        //{
        //    Ev_ARM2_Wafer_Back_FOUP_B = false;
        //}
        ///// <summary>
        ///// ARM2 Wafer放回 Foup报告
        ///// </summary>
        ///// <param name="sePresentData"></param>
        //public void Ev_ARM2_Wafer_Back_FOUP(SePresentData sePresentData)
        //{
        //    if (Ev_ARM2_Wafer_Back_FOUP_B) { return; }
        //    SetPresentValue(WorkStation_KE.Arm2, sePresentData);
        //    PosEvent(30034);
        //    Ev_ARM2_Wafer_Back_FOUP_B = true;
        //}

        bool Ev_AlignerGetWafer_B;
        public void Ev_AlignerGetWafer_R()
        {
            Ev_AlignerGetWafer_B = false;
        }
        /// <summary>
        /// Aligner取Wafer报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_AlignerGetWafer(SePresentData sePresentData)
        {
            if (Ev_AlignerGetWafer_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.Aligner, sePresentData);
            PosEvent(30033);
            Ev_AlignerGetWafer_B = true;
        }

        bool Ev_AlignerOutWafer_B;
        public void Ev_AlignerOutWafer_R()
        {
            Ev_AlignerOutWafer_B = false;
        }
        /// <summary>
        /// Aligner出Wafer报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_AlignerOutWafer(SePresentData sePresentData)
        {
            if (Ev_AlignerOutWafer_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.Aligner, sePresentData);
            PosEvent(30034);
            Ev_AlignerOutWafer_B = true;
        }

        bool Ev_InspectGetWafer_B;
        public void Ev_InspectGetWafer_R()
        {
            Ev_InspectGetWafer_B = false;
        }
        /// <summary>
        /// Inspect取Wafer报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_InspectGetWafer(SePresentData sePresentData)
        {
            if (Ev_InspectGetWafer_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspST, sePresentData);
            PosEvent(30035);
            Ev_InspectGetWafer_B = true;
        }

        bool Ev_InspectOutWafer_B;
        public void Ev_InspectOutWafer_R()
        {
            Ev_InspectOutWafer_B = false;
        }
        /// <summary>
        /// Inspect出Wafer报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_InspectOutWafer(SePresentData sePresentData)
        {
            if (Ev_InspectOutWafer_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspST, sePresentData);
            PosEvent(30036);
            Ev_InspectOutWafer_B = true;
        }


        bool Ev_InspectFolwStart_B;
        public void Ev_InspectFolwStart_R()
        {
            Ev_InspectFolwStart_B = false;
        }
        /// <summary>
        /// Inspect检测开始报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_InspectFolwStart(SePresentData sePresentData)
        {
            if (Ev_InspectFolwStart_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspST, sePresentData);
            PosEvent(30037);
            Ev_InspectFolwStart_B = true;
        }

        bool Ev_InspectFolwEnd_B;
        public void Ev_InspectFolwEnd_R()
        {
            Ev_InspectFolwEnd_B = false;
        }
        /// <summary>
        /// Inspect检测结束报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_InspectFolwEnd(SePresentData sePresentData)
        {
            if (Ev_InspectFolwEnd_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspST, sePresentData);
            PosEvent(30038);
            Ev_InspectFolwEnd_B = true;
        }

        //20240830 新增量测载台的两个事件
        bool Ev_InspCFolwStart_B;
        public void Ev_InspCFolwStart_R()
        {
            Ev_InspCFolwStart_B = false;
        }
        /// <summary>
        /// InspC检测开始报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_InspCFolwStart(SePresentData sePresentData)
        {
            if (Ev_InspCFolwStart_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspC, sePresentData);
            PosEvent(30039);
            Ev_InspCFolwStart_B = true;
        }



        bool Ev_InspCFolwEnd_B;
        public void Ev_InspCFolwEnd_R()
        {
            Ev_InspCFolwEnd_B = false;
        }
        /// <summary>
        /// InspC检测结束报告
        /// </summary>
        /// <param name="sePresentData"></param>
        public void Ev_InspCFolwEnd(SePresentData sePresentData)
        {
            if (Ev_InspCFolwEnd_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspC, sePresentData);
            PosEvent(30040);
            Ev_InspCFolwEnd_B = true;
        }




        bool Ev_AlignerReadWaferID_B;
        public void Ev_AlignerReadWaferID_R()
        {
            Ev_AlignerReadWaferID_B = false;
        }
        /// <summary>
        /// WaferID读取结果报告
        /// </summary>
        /// <param name="strWaferID"></param>
        /// <param name="sePresentData"></param>
        public void Ev_AlignerReadWaferID(string strWaferID, string strWaferID_Lot, SePresentData sePresentData)
        {
            if (Ev_AlignerReadWaferID_B) { return; }
            Variables.SetValue(10310, "0");
            Variables.SetPresentValue(WorkStation_KE.Aligner, sePresentData);
            Variables.SetValue(10430, strWaferID);
            Variables.SetValue(10435, strWaferID_Lot);
            PosEvent(30051);
            Ev_AlignerReadWaferID_B = true;
        }

        bool Ev_MEASURE_Result_Report_Item_B;
        public void Ev_MEASURE_Result_Report_Item_R()
        {
            Ev_MEASURE_Result_Report_Item_B = false;
        }
        /// <summary>
        /// 检测结果报告项方式1(多变量)
        /// </summary>
        /// <param name="sePresentData"></param>
        /// <param name="RecipeName"></param>
        /// <param name="portFoupID"></param>
        /// <param name="CheckPointPotion_X"></param>
        /// <param name="CheckPointPotion_Y"></param>
        /// <param name=""></param>
        public async void Ev_MEASURE_Result_Report_Item(SePresentData sePresentData, string RecipeName, string lotNO, string portFoupID, Dictionary<string, List<double>> CheckRes)
        {
            if (Ev_MEASURE_Result_Report_Item_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspST, sePresentData);
            Variables.SetValue(10350, RecipeName);
            Variables.SetValue(10351, lotNO);
            Variables.SetValue(10201, portFoupID);
            int n = 0;

            Task task1 = Task.Run(() =>
            {
                string strValue = "";
                foreach (var item in CheckRes)
                {
                    Variables.SetValue(11000 + n, item.Key);
                    Variables.SetValue(11001 + n, item.Value[0].ToString());
                    Variables.SetValue(11002 + n, item.Value[1].ToString());
                    Variables.SetValue(11003 + n, item.Value[2].ToString());
                    Variables.SetValue(11004 + n, item.Value[3].ToString());
                    n += 5;
                }
                if (n < 3000)
                {
                    for (int i = n; i < 3000; i += 5)
                    {
                        Variables.GetValue(11000 + i, ref strValue);
                        if (strValue.Equals("unuse"))
                        {
                            break;
                        }
                        Variables.SetValue(11000 + i, "unuse");
                        Variables.SetValue(11001 + i, "0");
                        Variables.SetValue(11002 + i, "0");
                        Variables.SetValue(11003 + i, "0");
                        Variables.SetValue(11004 + i, "0");
                    }
                }
            });
            await task1;
            PosEvent(30101);
            Ev_MEASURE_Result_Report_Item_B = true;
        }

        bool Ev_MEASURE_Result_Report_Item_2_B;
        public void Ev_MEASURE_Result_Report_Item_2_R()
        {
            Ev_MEASURE_Result_Report_Item_2_B = false;
        }
        /// <summary>
        /// 检测结果报告项方式2（单变量,SV.csv中对应变量类型为L）
        /// </summary>
        /// <param name="sePresentData"></param>
        /// <param name="RecipeName"></param>
        /// <param name="portFoupID"></param>
        /// <param name="CheckPointPotion_X"></param>
        /// <param name="CheckPointPotion_Y"></param>
        /// <param name=""></param>
        public void Ev_MEASURE_Result_Report_Item_2(SePresentData sePresentData, string RecipeName, string lotNO, string portFoupID, Dictionary<string, List<double>> CheckRes)
        {
            if (Ev_MEASURE_Result_Report_Item_2_B) { return; }
            Variables.SetPresentValue(WorkStation_KE.InspST, sePresentData);
            Variables.SetValue(10350, RecipeName);
            Variables.SetValue(10351, lotNO);
            Variables.SetValue(10201, portFoupID);

            string strCheckRes = Variables.ListJoinInspRes(CheckRes);
            Variables.SetValue(11000, strCheckRes);

            PosEvent(30101);
            Ev_MEASURE_Result_Report_Item_2_B = true;
        }

        #endregion

        private void LogEvenMsg(int intVID)
        {
            string strValue = "";
            string strPortID = "";
            if (Variables.IsContainRPTID(intVID, 40110) || Variables.IsContainVID(intVID, 10199))
            {
                Variables.GetValue(10199, ref strValue);
                if (strValue == "1")
                {
                    strPortID = "PA ->";
                }
                else
                {
                    strPortID = "PB ->";
                }
            }

            string strName = GetEventName(intVID);
            string strLog = $"{strPortID} {intVID}_{strName}";
            LogMsg("事件: " + strLog, MsgSource_KE.EQ);
        }

    }
}
